package managedbean;

import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.model.MenuModel;
import session.stateless.SystemMessagesBean;
import util.generator.TouristMenuGenerator;

@ManagedBean
@RequestScoped
public class IndexManagedBean implements Serializable
{
    @EJB
    private SystemMessagesBean systemMessages;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private String selectedLocale;
    private MenuModel touristMainMenuModel;
    private PageRedirectorBean redirect;
    
    public IndexManagedBean()
    {
        redirect = new PageRedirectorBean();
        
        // check if session has selectedLocale key alrd..
        // saved key exists then get from session and set to combo box
        if (ctx.getExternalContext().getSessionMap().containsKey("savedLocale")) {
            selectedLocale = ctx.getExternalContext().getSessionMap().get("savedLocale").toString();
//            System.out.println("Select 1: " + selectedLocale);
            if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
            else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
            else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
            else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
        }
        // key exists then get from session and set to combo box
        if (ctx.getExternalContext().getSessionMap().containsKey("selectedLocale")) {
            selectedLocale = ctx.getExternalContext().getSessionMap().get("selectedLocale").toString();
//            System.out.println("Select 2: " + selectedLocale);
            if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
            else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
            else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
            else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
        }
        // key does not exist then get from session and set to combo box
        else {
            ctx.getViewRoot().setLocale(new Locale("en", "US"));
            ctx.getExternalContext().getSessionMap().put("selectedLocale", ctx.getViewRoot().getLocale());
//            System.out.println("Select 3: " + ctx.getViewRoot().getLocale());
        }   
    }
    
    public void initView(PhaseEvent event) {
        // main menu 
        TouristMenuGenerator menuGenerator = new TouristMenuGenerator();
        touristMainMenuModel = menuGenerator.generateTouristPublicMainMenu();
    }
    
    public void selectedLocaleValueChangeListener()
    {
        System.out.println(selectedLocale);
        if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
        else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
        else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
        else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
        
        ctx.getExternalContext().getSessionMap().put("selectedLocale", ctx.getViewRoot().getLocale());
        
        // for the sake of non-ajaxable menus >.<
        refreshCurrentPage();
    }
    
    private void refreshCurrentPage() {
            try {
                String url = getRequestUrl();
                FacesContext.getCurrentInstance().getExternalContext().redirect(url);
            } catch (IOException ioe) {
                String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", selectedLocale);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
            }
    }
    
    private String getRequestUrl() {
        Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request instanceof HttpServletRequest) {
            return ((HttpServletRequest) request).getRequestURL().toString();
        } else {
            return "";
        }
    }
    
    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public String getSelectedLocale() {
        return selectedLocale;
    }

    public void setSelectedLocale(String selectedLocale) {
        this.selectedLocale = selectedLocale;
    }

    public MenuModel getTouristMainMenuModel() {
        return touristMainMenuModel;
    }

    public void setTouristMainMenuModel(MenuModel touristMainMenuModel) {
        this.touristMainMenuModel = touristMainMenuModel;
    }
   
}